package com.xm.servlet;

import com.xm.biz.GoodsBiz;
import com.xm.biz.SearchHistoryBiz;
import com.xm.biz.impl.GoodsBizImpl;
import com.xm.biz.impl.SearchHistoryBizImpl;
import com.xm.po.Goods;
import com.xm.po.SearchHistory;
import com.xm.po.Users;
import com.xm.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-12-10:22
 * @description 搜索处理器
 */
@WebServlet("/search")
public class SearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String searchInput = req.getParameter("index_none_header_sysc");
        String pageNo = req.getParameter("pageNo");
        String orderBy = req.getParameter("orderBy");
        GoodsBiz goodsBiz = new GoodsBizImpl();
        SearchHistoryBiz historyBiz = new SearchHistoryBizImpl();
        HttpSession session = req.getSession();
        //取出用户信息
        Users user = (Users) session.getAttribute("user");
        //判断是否是用户搜索
        if (user!=null){
            SearchHistory searchHistoryByUid = historyBiz.getSearchHistoryByUid(user.getUserId(), searchInput);
            //判断用户是否搜索重复
            if (searchHistoryByUid==null){
                SearchHistory searchHistory = new SearchHistory();
                searchHistory.setId(null);
                searchHistory.setSearchKey(searchInput);
                searchHistory.setCreateTime(new Date());
                searchHistory.setUserId(user.getUserId());
                searchHistory.setSearchType(null);
                searchHistory.setCount(1);
                historyBiz.add(searchHistory);
            }else {
                //搜索重复的修改数量
                searchHistoryByUid.setCount(searchHistoryByUid.getCount()+1);
                historyBiz.update(searchHistoryByUid);
            }
            List<SearchHistory> historyTop5 = historyBiz.getSearchHistoryTop5(user.getUserId());
            req.setAttribute("historyTop5",historyTop5);
        }else {
            //游客搜索记录
            SearchHistory searchHistory = new SearchHistory();
            searchHistory = new SearchHistory();
            searchHistory.setId(null);
            searchHistory.setSearchKey(searchInput);
            searchHistory.setCreateTime(new Date());
            searchHistory.setUserId(null);
            searchHistory.setSearchType(null);
            searchHistory.setCount(1);
            historyBiz.add(searchHistory);
        }
        PageBean<Goods> searchGoods = goodsBiz.searchGoods(searchInput,orderBy==null?orderBy="saleCount":orderBy, pageNo==null?1:Integer.valueOf(pageNo), 16);
        for (Goods goods : searchGoods.getData()) {
            if (goods.getGoodsName().indexOf(searchInput)!=-1){
                goods.setGoodsName(goods.getGoodsName().replace(searchInput,"<span style='color:red;'>"+searchInput+"</span>"));
            }
        }
        List<Goods> goodsByLike = goodsBiz.getGoodsByLike(searchInput);
        req.setAttribute("searchGoods",searchGoods);
        req.setAttribute("key",searchInput);
        req.setAttribute("goodsByLike",goodsByLike);
        req.getRequestDispatcher("search.jsp").forward(req,resp);
    }
}
